import { NameSpace } from 'utils/index';
import exceed from 'utils/apimap';
import dayjs from 'dayjs';

const { pathResource } = window.FORCE;
const ns = NameSpace('dashboardActivity');
export const RECEIVE_GROUP_ACTIVITIES = ns('RECEIVE_GROUP_ACTIVITIES');
export const LOADING_LIST = ns('LOADING_LIST');

// 获取用户activities
export function getGroupActivities(page, formData = {}) {
  return (dispatch) => {
    dispatch({
      type: LOADING_LIST,
    });

    if (formData.tb_user_ids) {
      formData.tb_user_ids = formData.tb_user_ids.join(',');
    }
    if (formData.range) {
      formData.start_time = dayjs(formData.range[0]).format('YYYY-MM-DD');
      formData.end_time = dayjs(formData.range[1]).format('YYYY-MM-DD');
    }
    delete formData.range;

    exceed
      .forceFetch({
        api: 'groups.all.activities',
        params: { groupId: pathResource.id },
        data: {
          page,
          per_page: 10,
          ...formData,
        },
      })
      .then((result) => {
        dispatch({
          type: RECEIVE_GROUP_ACTIVITIES,
          data: Object.assign(result, {
            currentPage: page,
          }),
        });
      });
  };
}
